﻿using Adnc.Shared.Events;
using DotNetCore.CAP;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Text.Json;
using System.Threading.Tasks;
using WYT.Usr.Application.Contracts.Dtos.GZH;
using WYT.Usr.Application.Contracts.Services;

namespace WYT.Usr.Application.EventSubscribers
{
    /// <summary>
    /// 
    /// </summary>
    public sealed class CapEventSubscriber : ICapSubscribe
    {
        private readonly IServiceProvider _services;
        private readonly ILogger<CapEventSubscriber> _logger;

        public CapEventSubscriber(
            IServiceProvider services
            , ILogger<CapEventSubscriber> logger)
        {
            _services = services;
            _logger = logger;
        }

        /// <summary>
        /// 订单商品出库
        /// </summary>
        /// <param name="eto"></param>
        /// <returns></returns>
        [CapSubscribe(nameof(OrderExwarehouseEvent))]
        public async Task ProcessOrderExwarehouseEvent(OrderExwarehouseEvent eto)
        {
            var data = eto.Data;
            using var scope = _services.CreateScope();
            var appSrv = scope.ServiceProvider.GetRequiredService<IWarehouseAppService>();
            await appSrv.InventoryManagement(data.OrderId);
        }

        /// <summary>
        /// 用户升级检查
        /// </summary>
        /// <param name="eto"></param>
        /// <returns></returns>
        [CapSubscribe(nameof(UserUpgradeCheckEvent))]
        public async Task ProcessUserUpgradeCheckEvent(UserUpgradeCheckEvent eto)
        {
            var data = eto.Data;
            using var scope = _services.CreateScope();
            var appSrv = scope.ServiceProvider.GetRequiredService<IUserAppService>();
            await appSrv.UserUpgradeCheck(new Contracts.Dtos.User.UserUpgradeCheckDto { OrderId = data.OrderId, UserIds = data.UserIds });
        }


        /// <summary>
        /// 给用户发送公众号消息
        /// </summary>
        /// <param name="eto"></param>
        /// <returns></returns>
        [CapSubscribe(nameof(UserGZHMessagePushEvent))]
        public async Task GZHMessagePush(UserGZHMessagePushEvent eto)
        {
            var data = eto.Data;
            using var scope = _services.CreateScope();
            var appSrv = scope.ServiceProvider.GetRequiredService<IGZHAppService>();
            await appSrv.PushMessage(new MessagePushDto { UserId = data.UserId,TemplateId = data.TemplateId,Pagepath = data.Pagepath,Data = JsonSerializer.Serialize(data.Data)});
        }

        /// <summary>
        /// 订单收货后处理
        /// </summary>
        /// <param name="eto"></param>
        /// <returns></returns>
        [CapSubscribe(nameof(OrderReceivingEvent))]
        public async Task OrderReceivingEventHandle(OrderReceivingEvent eto)
        {
            var data = eto.Data;
            using var scope = _services.CreateScope();
            var appSrv = scope.ServiceProvider.GetRequiredService<IWarehouseAppService>();
            await appSrv.OrderReceivingHandle(data.OrderId);            
        }
        
    }
}
